package com.family.dict;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Component;


@Component("dict")
public class FeatureDict {
	
	private HashMap<String,Integer> hash2 = new HashMap<String,Integer>();
	private HashMap<String,Integer> hash3 = new HashMap<String,Integer>();
	private HashMap<String,Integer> hash4 = new HashMap<String,Integer>();
	private HashMap<String,Integer> hash5 = new HashMap<String,Integer>();
	
	
	//private String file1 = FeatureDict.class.getClassLoader().getResource("dict/feature.txt").getPath();
	String file1 = "dict/feature.txt";
	
	FeatureDict(){
//		this.readFile(file1);
	}
	
	public void readFile(String filePath){
		File file = new File(filePath);
        BufferedReader reader = null;
        try {
          
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
            	if(tempString.trim().equals("")||tempString.startsWith("#")) continue;
            	String term = tempString.trim();
            	if(term.length()<2) continue;
            	
            	if(term.length()>=5){
            		hash5.put(term, 1);
            	}else if(term.length()==4){
            		hash4.put(term, 1);
            	}else if(term.length()==3){
            		hash3.put(term, 1);
            	}else if(term.length()==2){
            		hash2.put(term, 1);
            	}
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }   
	}
	
	public int featureMeasure(String content){
		int total = 0;
		
		Set<String> keys5 = this.hash5.keySet();
		for(java.util.Iterator<String> it=keys5.iterator();it.hasNext();){
			String key = it.next();
			if(content.contains(key)){
				int value = this.hash5.get(key);
				total += value;
				content = content.replaceFirst(key, " ***** ");
			}
		}
		Set<String> keys4 = this.hash4.keySet();
		for(java.util.Iterator<String> it=keys4.iterator();it.hasNext();){
			String key = it.next();
			if(content.contains(key)){
				int value = this.hash4.get(key);
				total += value;
				content = content.replaceFirst(key, " ***** ");
			}
		}
		Set<String> keys3 = this.hash3.keySet();
		for(java.util.Iterator<String> it=keys3.iterator();it.hasNext();){
			String key = it.next();
			if(content.contains(key)){
				int value = this.hash3.get(key);
				total += value;
				content = content.replaceFirst(key, " ***** ");
			}
		}
		Set<String> keys2 = this.hash2.keySet();
		for(java.util.Iterator<String> it=keys2.iterator();it.hasNext();){
			String key = it.next();
			if(content.contains(key)){
				int value = this.hash2.get(key);
				total += value;
				content = content.replaceFirst(key, " ***** ");
			}
		}
		return total;
	}
	
	public List<String> featureExtract(String content){
		List total = new ArrayList<String>();

		Set<String> keys5 = this.hash5.keySet();
		for(java.util.Iterator<String> it=keys5.iterator();it.hasNext();){
			String key = it.next();
			if(content.contains(key)){
				int value = this.hash5.get(key);
				if(!total.contains(key))total.add(key);
				content = content.replaceFirst(key, " ***** ");
			}
		}
		Set<String> keys4 = this.hash4.keySet();
		for(java.util.Iterator<String> it=keys4.iterator();it.hasNext();){
			String key = it.next();
			if(content.contains(key)){
				int value = this.hash4.get(key);
				if(!total.contains(key))total.add(key);
				content = content.replaceFirst(key, " ***** ");
			}
		}
		Set<String> keys3 = this.hash3.keySet();
		for(java.util.Iterator<String> it=keys3.iterator();it.hasNext();){
			String key = it.next();
			if(content.contains(key)){
				int value = this.hash3.get(key);
				if(!total.contains(key))total.add(key);
				content = content.replaceFirst(key, " ***** ");
			}
		}
		Set<String> keys2 = this.hash2.keySet();
		for(java.util.Iterator<String> it=keys2.iterator();it.hasNext();){
			String key = it.next();
			if(content.contains(key)){
				int value = this.hash2.get(key);
				if(!total.contains(key))total.add(key);
				content = content.replaceFirst(key, " ***** ");
			}
		}
		return total;
	}
	
	public static void main(String[] args){
		String content = "最近股票跌的比较厉害，不知道什么时候能变好爆营收下降 中国，金融家";
		FeatureDict dic = new FeatureDict();
		
		int stNu = dic.featureMeasure(content);
		System.out.println(stNu);
	}
	
	
	
}
